"""
This file is part of GlToolkit project https://code.google.com/p/gltoolkit/

Copyright (C) 2009-2010 Synble Ltd.
All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
"""
from optparse import OptionParser
from glapi import *

class CliMain():
    silent = False
    
    def cprint(self, s):
        if not self.silent:
            print s;
    
    def error(self, s):
        print "error: %s" % s;
            
    def cli(self, args, opts):
        glApi = GLApi()

        username = opts.get('username')
        password = opts.get('password')
        silent = opts.get('verbose')

        glApi.setUsername(username)
        glApi.setPassword(password)

        if args[0] == "upload":
            if len(args) < 2:
                self.error("Please specify the file you want to upload.")
                return
            if len(args) < 3:
                self.error("Can't upload to unknown product. It's your GL product name, same as in URL.")
                return
            if len(args) < 4:
                self.error("Language is missing. It should be standard IANA code e.g. en, en-US or fi. If you are uploading master strings, use master.")
                return

            component = "default"

            if len(args) > 4:
                component = args[4]
            
            glApi.uploadFile(args[1], args[2], args[3], component)

        if args[0] == "get":
            if len(args) < 2:
                self.error("Can't download from unknown product. It's your GL product name, same as in URL.")
                return
            if len(args) < 3:
                self.error("Language is missing. It should be standard IANA code e.g. en, en-US or fi. If you are uploading master strings, use master.")
                return
            component = "default"

            if len(args) > 3:
                component = args[3]
            glApi.getFile(args[1], args[2], component)

        return None
